package com.tangyuan.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tangyuan.common.result.Result;
import com.tangyuan.common.result.ResultCodeEnum;
import com.tangyuan.model.activity.HonouredGuest;
import com.tangyuan.model.activity.PollRecord;
import com.tangyuan.model.activity.UserActivity;
import com.tangyuan.user.mapper.PollRecordMapper;
import com.tangyuan.user.service.IHonouredGuestService;
import com.tangyuan.user.service.IPollRecordService;
import com.tangyuan.user.service.IUserActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description: poll_record
 * @Author: jeecg-boot
 * @Date: 2024-03-20
 * @Version: V1.0
 */
@Service
public class PollRecordServiceImpl extends ServiceImpl<PollRecordMapper, PollRecord> implements IPollRecordService {

    @Autowired
    private IHonouredGuestService honouredGuestService;
    @Autowired
    private IUserActivityService userActivityService;


    @Override
    public Result<Object> getPollByUserId(Long userId, PollRecord pollRecord) {
        if (userId==null){
            return Result.build(null, ResultCodeEnum.LOGIN_AUTH);
        }
        if (pollRecord == null){
            return Result.build(null, ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        if (pollRecord.getActivityId()==null){
            return Result.build(null, ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }
        UserActivity userActivity = userActivityService.getById(pollRecord.getActivityId());
        if (userActivity == null){
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
        //之后
        if (userActivity.getStartTime().after(new Date())){
            return Result.build(null, ResultCodeEnum.ACTIVITY_HAS_NOT_STARTED);
        }
        //之前
        if (userActivity.getEndTime().before(new Date())){
            return Result.build(null, ResultCodeEnum.ACTIVE_OVER_TIME);
        }
        PollRecord record = this.getOne(new LambdaQueryWrapper<PollRecord>()
                .eq(PollRecord::getActivityId, pollRecord.getActivityId())
                .eq(PollRecord::getUserId, userId));
        if (record != null) {
            if (record.getVoteId().equals(pollRecord.getVoteId())) {
                return Result.build(null, ResultCodeEnum.MORE_THAN_ONCE);
            } else {
                return Result.build(null, ResultCodeEnum.YOU_HAVE_VOTED);
            }
        }
        pollRecord.setUserId(userId);
        this.save(pollRecord);
        LambdaQueryWrapper<HonouredGuest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HonouredGuest::getActivityId, pollRecord.getActivityId());
        queryWrapper.eq(HonouredGuest::getNumberId, pollRecord.getVoteId());
        HonouredGuest guest = honouredGuestService.getOne(queryWrapper);
        guest.setVoteNum(guest.getVoteNum() + pollRecord.getViteNum());
        honouredGuestService.updateById(guest);
        UserActivity activity = userActivityService.getById(pollRecord.getActivityId());
        activity.setAttendance(activity.getAttendance()+1);
        activity.setPollNum(activity.getPollNum() + pollRecord.getViteNum());
        userActivityService.updateById(activity);
        return Result.ok("投票成功！");
    }
}
